// https://www.geeksforgeeks.org/josephus-problem-set-1-a-on-solution/
#include <iostream>
using namespace std;
 
int n, k;
int josephus(int n, int depth, int k=2)
{
    if (n == 1)
        return 1;
    else
        /* The position returned by josephus(n - 1, k)
        is adjusted because the recursive call 
        josephus(n - 1, k) considers the 
        original position k % n + 1 as position 1 */
        return (josephus(n - 1, depth+1) + k-1) % n + 1;
}
 
// Driver Program to test above function
int main()
{
    int tc; cin>>tc;
    for(int i=0; i<tc; ++i) {
      // cout << "The chosen place is " << josephus(n, k)<<'\n';
      cin>>n>>k;
      cout<<josephus(n, 1)<<'\n';
    }
    return 0;
}
 
// This code is contributed by shubhamsingh10
